package com.yd.app.mapper;

import com.yd.app.mapper.provider.MessageProvider;
import com.yd.domain.business.Message;
import com.yd.domain.business.UserMsg;
import com.yd.domain.searchParam.MessageParam;
import com.yd.domain.searchParam.MessageReturnParam;
import com.yd.domain.sys.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zy
 * @create 2018-03-13 9:11
 * @desc 信息交流
 **/
@Repository
public interface MessageMapper {
    /**
     * @Author: zy
     * @Desc: 新增信息交流
     * @Date:  2018/3/13
     * @Param
     */
    @Insert("INSERT INTO business_message (id,content,publishTime,publishUser,receiveUser,token,type)" +
            " VALUES(#{m.id},#{m.content},#{m.publishTime},#{m.publishUser},#{m.receiveUser},#{m.token},#{m.type})")
    void addMessage(@Param("m")Message m);
    /**
     * @Author: zy
     * @Desc: 修改数据
     * @Date:  2018/3/13
     * @Param
     */
    @Update("update business_message set content=#{m.content},publishTime=#{m.publishTime},publishUser=#{m.publishUser}," +
            "receiveUser=#{m.receiveUser},token=#{m.token} where id=#{m.id}")
    void updateMessage(@Param("m")Message m);
    /**
     * @Author: zy
     * @Desc: 根据id删除
     * @Date:  2018/3/13
     * @Param
     */
    @Delete("delete from business_message where id=#{id}")
    void deleteMessage(@Param("id") String id);
    /**
     * @Author: zy
     * @Desc: 根据id查询
     * @Date:  2018/3/13
     * @Param
     */
    @Select("SELECT m.id,m.content,m.publishTime,m.publishUser,m.receiveUser,m.token  from business_message m  WHERE m.id=#{id}")
    Message findById(@Param("id") String id);
    /**
     * @Author: zy
     * @Desc: 分页查询
     * @Date:  2018/3/13
     * @Param
     */
    @SelectProvider(method = "searchMessageList",type= MessageProvider.class)
    List<Message> searchMessageList(@Param("messageParam") MessageParam messageParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc: 查询数据总数量
     * @Date:  2018/3/13
     * @Param
     */
    @SelectProvider(method = "searchMessageCount",type= MessageProvider.class)
    int searchMessageCount(@Param("messageParam") MessageParam messageParam);
    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/13
     * @Param
     */
    @SelectProvider(method = "deleteAll",type=MessageProvider.class)
    void deleteAll(@Param("message")Message message);

    /**
     * 获取 我的聊天列表
     * @param userId
     * @return
     */
    @Select("select sum(if(ifRead='0',1,0)) as count,token,publishUser,sys_user.realName from business_message " +
            " left join sys_user on publishUser=sys_user.id where  receiveUser=#{userId}  GROUP BY token,publishUser  ")
    List<MessageReturnParam> getMessageForList(@Param("userId")String userId);
   /* *//**
     * 获取 管理员 的聊天列表
     * @param userId
     * @return
     *//*
    @Select("select sum(if(ifRead='0',1,0)) as count,token,publishUser,sys_user.realName from business_message " +
            " left join sys_user on publishUser=sys_user.id   GROUP BY token  ")
    List<MessageReturnParam> getMessageForListWithManager(@Param("userId")String userId);

   */

    /**
     * 查询谈话记录
     * @param token
     * @param startRow
     * @param pageSize
     * @return
     */
    @SelectProvider(method = "searchMessageTalkList",type= MessageProvider.class)
    List<Message> searchMessageTalkList(@Param("token")String token,@Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * 查询谈话记录数量
     * @param token
     * @return
     */
    @SelectProvider(method = "searchMessageTalkCount",type= MessageProvider.class)
    int searchMessageTalkCount(@Param("token")String token);

    /*
    //获取我的未读信息数量
     */
    @Select("select count(1) from business_message where receiveUser=#{userId} and ifRead='0' ")
    int getNoReadMessageCount(String userId);

    /**
     * 更新已读状态
     * @param ifRead
     * @param ifRead
     */
    @Update("update business_message set ifRead=#{ifRead} where token=#{token} and receiveUser=#{receiveUser} ")
    void updateMessageIfRead(@Param("receiveUser")String receiveUser,@Param("token")String token,@Param("ifRead") boolean ifRead);

    /**
     * 查询最新消息
     * @param token
     * @return
     */
    @Select("select * from business_message where token=#{token} order by publishTime desc limit 1 ")
    Message getLastMessage(@Param("token")String token);

    /**
     * 删除交谈信息
     * @param token
     */
    @Delete("delete from business_message where token=#{token} ")
    void deleteTalk(@Param("token")String token);
    /**
     * @Author: zy
     * @Desc: 获取客服人员
     * @Date:  2018/4/9
     * @Param
     */
    @Select("SELECT u.* from sys_user u where u.id in (SELECT DISTINCT ur.userId from sys_user_role ur  where ur.roleCode in (SELECT DISTINCT rp.roleId from sys_role_perm rp where rp.permCode='messageManager'))")
    List<User> getAllCustomService();


    /**
     * 获取是否存在最新消息
     * @param token
     * @return
     */
    @Select("select *  from business_user_msg where token=#{token} and receiveUser=#{receiveUser} ")
    UserMsg getIfMsg(@Param("token") String token, @Param("receiveUser") String receiveUser);

    /**
     * 获取最新消息未读消息
     * @param token
     * @param receiveUser
     * @return
     */
    @Select("select * from business_message where token=#{token} and receiveUser=#{receiveUser} and ifRead=false ")
    List<Message> getLastMsg(@Param("token") String token,@Param("receiveUser") String receiveUser);

    /**
     * 更新已读状态
     */
    @Update("update business_message set ifRead=true where token=#{token} and receiveUser=#{receiveUser} ")
    void updateMessageIfReadWithReceiveUser(@Param("token") String token,@Param("receiveUser")  String receiveUser);

    /**
     * 更新是否含有最新消息
     * @param token
     * @param receiveUser
     */
    @Update("update business_user_msg set ifMsg=false where token=#{token} and receiveUser=#{receiveUser} ")
    void updateIfMsgWithReceiveUser(@Param("token") String token, @Param("receiveUser") String receiveUser);

    /**
     * 更新是否含有最新消息
     * @param userMsg
     */
    @Insert("insert into business_user_msg(token,publishUser,receiveUser,ifMsg) values(#{um.token},#{um.publishUser},#{um.receiveUser},#{um.ifMsg})  ON DUPLICATE KEY UPDATE `ifMsg`=values(ifMsg) ")
    void addOrUpdate(@Param("um") UserMsg userMsg);

    /**
     * 根据token清空聊天记录
     * @param token
     */
    @Delete("delete  from business_message  where token=#{token} ")
    void cleanMessage(@Param("token") String token);

    @Select("select sum(if(ifRead='0',1,0)) as count,token,publishUser,sys_user.realName from business_message " +
            " left join sys_user on publishUser=sys_user.id where  receiveUser=#{userId} and token=#{token} GROUP BY token,publishUser limit 1 ")
    MessageReturnParam getMessageForListByToken(@Param("token") String token,@Param("userId") String loginUserId);

}
